<?php
/*
Coder: NetPuter
Description: 一些功能, 方便调用
*/
header("Content-Type: text/html; charset=utf-8");
define("ABSPATH", dirname(__FILE__)."/");

// 通过 Cookies 记录城市
if (!isset($_COOKIE["cookieCity"])) {
	$ipCity = location(getIP());
	setcookie("cookieCity", $ipCity, time() + 3600*24*365);
}
$userCity = $_COOKIE["cookieCity"];

// 切换城市时保存到 Cookies 中
function setCookieCity($city) {
	setcookie("cookieCity", $city, time() + 3600*24*365);
	return true;
}

// 将城市及分类拼音转换为中文
function translate($query) {
	$cityConfig = file(ABSPATH."list/citylist");
	array_splice($cityConfig, 0, 1).array_splice($cityConfig, -1, 1);
	$filter = array("'", ",");

	for ($i=1;$i<count($cityConfig);$i=$i+2) {
		$cityFormat = str_replace($filter, "", $cityConfig[$i]);
		$citySplit = explode(":", trim($cityFormat));
		$array[$citySplit[0]] = $citySplit[1];
		$array[$citySplit[1]] = $citySplit[0];
	}

	$array["all"] = "所有";
	$array["eat"] = "吃喝";
	$array["play"] = "玩乐";
	$array["beauty"] = "装扮";
	$array["sports"] = "运动";
	$array["person"] = "个人";
	$array["other"] = "其它";

	if (isset($array[$query])) {
		return $array[$query];
	} else {
		return false;
	}
}

// 获取用户 IP 及其对应的城市, 来自 仿8684判断用户所在城市
function getIP() {
	$ip = false;
	if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
		$ip = $_SERVER["HTTP_CLIENT_IP"];
	}
	if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
		$ips = explode(",", $_SERVER['HTTP_X_FORWARDED_FOR']);
		if ($ip) {
			array_unshift($ips, $ip);
			$ip = FALSE;
		}
		for ($i=0;$i<count($ips);$i++) {
			if (!eregi("^(10│172.16│192.168).",$ips[$i])) {
				$ip = $ips[$i];
				break;
			}
		}
	}
	$ip= $ip ? $ip : $_SERVER['REMOTE_ADDR'];
}

function location($ip) {
	$city = file_get_contents("http://app.orzdream.com/ip2city.php?ip=$ip");
	return $city;
}

// 获取城市天气
function weather($city) {
	$content = file_get_contents("http://www.google.com/ig/api?hl=zh-cn&weather=$city");
	$content = mb_convert_encoding($content, "UTF-8", "GBK");
	$xml = simplexml_load_string($content);

	if (isset($xml->weather->problem_cause)) {
		$error = str_replace("关信", "关天气信", $xml->weather->problem_cause->attributes());
		return $error;
	}

	$cTemp = $xml->weather->current_conditions->temp_c->attributes();
	$cCondition = $xml->weather->current_conditions->condition->attributes();
	$cHumidity = str_replace("：", "", $xml->weather->current_conditions->humidity->attributes());

	foreach ($xml->weather->forecast_conditions as $forecast) {
		$low = $forecast->low->attributes();
		$high = $forecast->high->attributes();
		$condition = $forecast->condition->attributes();
		$info[] = "{$low}-{$high}℃（{$condition}）";
	}

	$weather = translate($city)."，{$cTemp}℃（{$cCondition}），{$cHumidity} 。预计明天 {$info[0]}，后天 {$info[1]}。";
	
	return $weather;
}

// 前端顶部
function get_header($title) { ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>种子团 &rsaquo; <?php echo $title; ?></title>
	<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<p>I'm Header.</p>
<blockquote><?php echo location(getIP()); ?></blockquote>
<? }

// 前端底部
function get_footer() { ?>
<p>I'm Footer.</p>
</body>
</html>
<? }
?>